package com.xjoyt.pay.master.core.order;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.gateway.params.OrderCreateParam;
import com.xjoyt.pay.master.core.order.handle.AbstractOrderCreateHandle;
import com.xjoyt.pay.master.core.order.handle.create.CreateAlipayScanHandle;
import com.xjoyt.pay.master.core.order.handle.create.SaveHandle;
import com.xjoyt.pay.master.core.order.handle.create.ValidHandle;
import com.xjoyt.pay.master.dto.OrderUrlResult;
import com.xjoyt.pay.vo.PayType;

/**
 * 下单处理
 * 
 * @author Administrator
 *
 */
@Component
public class OrderAlipayScanCreateHandle {

	private AbstractOrderCreateHandle startHandle;

	@Autowired
	private ValidHandle validHandle;

	@Autowired
	private SaveHandle saveHandle;

	@Autowired
	private CreateAlipayScanHandle createHandle;

	private AbstractOrderCreateHandle getStartHandle() {
		if (startHandle != null) {
			return startHandle;
		}

		// 验证订单
		validHandle.setNext(saveHandle);

		// 保存订单
		saveHandle.setNext(createHandle);

		startHandle = validHandle;
		return startHandle;
	}

	public ExecuteResult<OrderUrlResult> handle(OrderCreateParam param, PayType payType) {
		Map<String, Object> args = new HashMap<>();
		return getStartHandle().handle(param, payType, args);
	}
}
